﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the L, a, b values are filtered instead of R G B
    /// </summary>
    public class HCLFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_HInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_CInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_LInterval = new DoubleInterval(0.0f, 1.0f);

        /// <summary>
        /// default constructor
        /// </summary>
        public HCLFilter()
        {
        }

        /// <summary>
        /// specify intervals directly
        /// </summary>
        /// <param name="_h"></param>
        /// <param name="_c"></param>
        /// <param name="_l"></param>
        public HCLFilter(DoubleInterval _h, DoubleInterval _c, DoubleInterval _l)
        {
            m_HInterval = _h;
            m_CInterval = _c;
            m_LInterval = _l;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_hclfilter"></param>
        internal HCLFilter(HCLFilter _hclfilter)
        {
            this.m_HInterval = _hclfilter.m_HInterval;
            this.m_CInterval = _hclfilter.m_CInterval;
            this.m_LInterval = _hclfilter.m_LInterval;
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public DoubleInterval H
        {
            get { return m_HInterval; }
            set { m_HInterval = value; }
        }

        /// <summary>
        /// Gets or sets C
        /// </summary>
        public DoubleInterval C
        {
            get { return m_CInterval; }
            set { m_CInterval = value; }
        }

        /// <summary>
        /// Gets or sets L
        /// </summary>
        public DoubleInterval L
        {
            get { return m_LInterval; }
            set { m_LInterval = value; }
        }


        /// <summary>
        /// executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            HCL hsb = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    hsb = new HCL(bitmap.GetPixel(x, y));

                    if (m_HInterval.Contains(hsb.H)
                        && m_CInterval.Contains(hsb.C)
                        && m_LInterval.Contains(hsb.L))
                    {
                        bitmap.SetPixel(x, y, hsb.ToColor());
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HCLFilter(this);
        }
    }
}
